package LeetCode.剑指Offer._220531;

import com.sun.xml.internal.fastinfoset.util.CharArray;

/**
 * @Author: yqy
 * @Date: 2022/05/31/16:11
 * @Description:验证回文串
 */
public class _125 {
    public static void main(String[] args) {
        boolean b = isPalindrome("A man, a plan, a canal: Panama");
        System.out.println(b);
    }

    //双指针，只要是数字和字母就往前面移动
    public static boolean isPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        boolean flag = true;
        while (i < j) {
            //只要不是是字母数字，前加后减
            while ((i < j) && !Character.isLetterOrDigit(s.charAt(i))) {
                i++;
            }
            while ((i < j) && !Character.isLetterOrDigit(s.charAt(j))) {
                j--;
            }
            if (Character.toLowerCase(s.charAt(i))!=Character.toLowerCase(s.charAt(j))) {
                flag = false;
                break;
            }
            i++;
            j--;
        }
        return flag;
    }

    public static boolean isPalindromePlus(String s) {
        StringBuffer str=new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isLetterOrDigit(c)){
                str.append(Character.toLowerCase(c));
            }
        }
        String before=str.toString();
        str.reverse();
        return before.equals(str.toString()) ? true: false;
    }
}
